package com.ymatou.iapi.livemanage.testcase;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livemanage.service.ConfirmActivityCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.database.mongo.LiveBasicInfoWapper;
import com.ymttest.database.mongo.LiveModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.LivesWapper;
import com.ymttest.database.sqlwapper.YmtLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class Ts_ConfirmActivity {
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播Admin-定位直播");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new ConfirmActivityCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	
	 @Test
	 @Category(P1.class)
	 @TestCase
	public void Tc_ConfirmActivity_001() {
		 Logger.start(true,"直播Admin-定位直播");
		 try {
			 String strLatlng = "111,101";
			 String positionStr = "shanghaishizhabeiqu";
			 JSONObject liveBean = LiveCallService.createLiveNotInEffect(sellerName,sellerId);
			 Logger.verifyEquals(200,liveBean.getInt("Code"),  "响应报文-返回码");	
			 int liveId = liveBean.getJSONObject("Data").getInt("ActivityId");
			 
			 Map<String,Object> updateMap = new HashMap<String,Object>();
			 updateMap.put("SellerId", sellerId);
			 updateMap.put("ActivityId", liveId);
			 updateMap.put("Position", positionStr);
			 updateMap.put("Latlng", strLatlng);
			 
//			Thread.sleep(10000);
			
			 JSONObject confirmBean = new ConfirmActivityCall().callAndGetReturnData(updateMap);
			 Logger.verifyEquals(200,confirmBean.getInt("Code"),  "响应报文-返回码");	
			 
			 
			 Map<String, Object> tgtRetMap = new HashMap<String,Object>();
			 //check SQL
			 Map<String,Object> ret = MapUtil.pojoToMap(new YmtLiveWapper().selectByLiveId(liveId));
			 tgtRetMap.clear();
			 tgtRetMap.put("sposition", positionStr);
			 tgtRetMap.put("bappconfirmed", true);
			 tgtRetMap.put("slatlng", strLatlng);
			 AssertService.assertResultEqual(tgtRetMap, ret,"SQL-YmtActivity");
			 
			 //check Mongo
			 if (YmtMongoDriverFactroy.isOldMongoCheck()){
				 Map<String, Object>liveBasicInfoMap = new LiveBasicInfoWapper().getActivityId(liveId);
				 tgtRetMap.clear();
				 tgtRetMap.put("ShopAddress", positionStr);	
				 tgtRetMap.put("AppConfirmed", true);	
				 tgtRetMap.put("Latlng", strLatlng);	
				 AssertService.assertResultEqual(tgtRetMap, liveBasicInfoMap,"Mongo-liveBasic");
			 	}
			 
			 if (YmtMongoDriverFactroy.isNewMongoCheck()){
				 Map<String, Object>liveModelWapperMap = new LiveModelWapper().getActivityId(liveId);
				 tgtRetMap.clear();
				 tgtRetMap.put("ShopAddress", positionStr);
				 tgtRetMap.put("AppConfirmed", true);
				 tgtRetMap.put("Latlng", strLatlng);	
				 AssertService.assertResultEqual(tgtRetMap, liveModelWapperMap,"Mongo-liveModel");
			 }
			 
			 if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				 Map<String, Object>liveModelWapperMap = new LivesWapper().getByActivityId(liveId);
				 tgtRetMap.clear();
				 tgtRetMap.put("addr", positionStr);
				 tgtRetMap.put("confirm", true);
				 tgtRetMap.put("latlng", strLatlng);	
				 AssertService.assertResultEqual(tgtRetMap, liveModelWapperMap,"Mongo-Lives");
				 NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(liveId);
			 }
			
			
			
			 
		 }catch(Exception e){
			 Logger.fail(e);
		 }
	 }
	 
	 @Test
	 @Category(P1.class)
	 @TestCase
	public void Tc_ConfirmActivity_002() {
		 Logger.start(true,"已生效直播再次定位直播");
		 try {			 
			 
			String positionStr = "shanghaishizhabeiqu";
			JSONObject liveBean = LiveCallService.createLiveBeginInFuture(sellerName,sellerId);
			Logger.verifyEquals(200,liveBean.getInt("Code"),  "响应报文-返回码");	
			int liveId = liveBean.getJSONObject("Data").getInt("ActivityId");
			 
			Map<String,Object> updateMap = new HashMap<String,Object>();
			updateMap.put("SellerId", sellerId);
			updateMap.put("ActivityId", liveId);
			updateMap.put("Position", positionStr);
			 
			JSONObject confirmBean = new ConfirmActivityCall().callAndGetReturnData(updateMap);
			Logger.verifyEquals(201,confirmBean.getInt("Code"),  "响应报文-返回码");	
			 
			Map<String, Object> tgtRetMap = new HashMap<String,Object>();
			 //check SQL
			Map<String,Object> ret = MapUtil.pojoToMap(new YmtLiveWapper().selectByLiveId(liveId));
			tgtRetMap.clear();
			tgtRetMap.put("sposition", "闸北");
			tgtRetMap.put("bappconfirmed", true);
			AssertService.assertResultEqual(tgtRetMap, ret);
			 
			 //check Mongo
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				Map<String, Object>liveBasicInfoMap = new LiveBasicInfoWapper().getActivityId(liveId);
				tgtRetMap.clear();
				tgtRetMap.put("ShopAddress", "闸北");	
				tgtRetMap.put("AppConfirmed", true);	
				AssertService.assertResultEqual(tgtRetMap, liveBasicInfoMap);
			 }
			 
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				Map<String, Object>liveModelWapperMap = new LiveModelWapper().getActivityId(liveId);
				tgtRetMap.clear();
				tgtRetMap.put("ShopAddress", "闸北");
				tgtRetMap.put("AppConfirmed", true);
				AssertService.assertResultEqual(tgtRetMap, liveModelWapperMap);
			 }
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				Map<String, Object>liveModelWapperMap = new LivesWapper().getByActivityId(liveId);
				tgtRetMap.clear();
				tgtRetMap.put("addr", "闸北");
				tgtRetMap.put("confirm", true);
				AssertService.assertResultEqual(tgtRetMap, liveModelWapperMap);
				NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(liveId);
			 }
			
			
			 
		 }catch(Exception e){
			 Logger.fail(e);
		 }
	 }	 
	 
	 
	 
	 
}
